package com.wgchao.authmanager.infrastructure.convert.em;

import com.wgchao.authmanager.domain.model.Agency;
import com.wgchao.authmanager.domain.model.Operator;
import com.wgchao.authmanager.infrastructure.convert.Convert;
import com.wgchao.authmanager.infrastructure.jpa.entity.AgencyInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

/**
 * @ClassName: AgencyInfoConvert
 * @author: wgchao
 * @createTime: 2020/4/29 3:30 PM
 * @Description: TODO
 */
@Component
public class AgencyInfoConvert implements Convert<Agency, AgencyInfo> {

    @Override
    public AgencyInfo toEntity(Agency agency) {
        AgencyInfo agencyInfo = new AgencyInfo();
        BeanUtils.copyProperties(agency, agencyInfo);
        if(agency.getCreator() != null){
            agencyInfo.setCreateUser(agency.getCreator().getUname());
            agencyInfo.setCreateTime(agency.getCreator().getOperateTime());
        }
        if(agency.getLastupdator() != null){
            agencyInfo.setLastUpdateUser(agency.getLastupdator().getUname());
            agencyInfo.setLastUpdateTime(agency.getLastupdator().getOperateTime());
        }
        agencyInfo.setId(agency.findId());
        agencyInfo.setVersion(agency.findVersion());
        return agencyInfo;
    }

    @Override
    public Agency toModel(AgencyInfo agencyInfo) {
        return new Agency(agencyInfo.getId(), agencyInfo.getVersion(),
                agencyInfo.getAgencyCode(), agencyInfo.getAgencyName(),
                agencyInfo.getRemark(), agencyInfo.isDisabled(),
                new Operator(agencyInfo.getCreateUser(), agencyInfo.getCreateTime()),
                new Operator(agencyInfo.getLastUpdateUser(), agencyInfo.getLastUpdateTime()));
    }
}
